package com.css.bjdt_core.validation;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mvel2.MVEL;

import com.css.bjdt_core.cell.entity.Cell;
import com.css.bjdt_core.cell.entity.Cell.CellType;
import com.css.bjdt_core.cell.entity.CellData;
import com.css.bjdt_core.cell.service.CellDataService;
import com.css.bjdt_core.executive.entity.Exp;
import com.css.bjdt_core.executive.report.ReportCache;
import com.css.bjdt_core.explain.entity.RptTemplateExplain;
import com.css.bjdt_core.explain.service.ReportTemplateExplainService;
import com.css.bjdt_core.report.entity.RptTemplateUnitRela;
import com.css.bjdt_core.report.entity.RptTemplateValidation.ValidationType;
import com.css.bjdt_core.report.service.ReportTemplateValidationService;
import com.css.bjdt_core.reportPrepare.entity.RptBatchTemplateRel;
import com.css.bjdt_core.reportPrepare.service.RptDealWithService;
import com.css.bjdt_core.util.ExecutorPool;
import com.css.bjdt_core.util.ReportConstant;
import com.css.bjdt_core.validation.entity.ValidationInfo;
import com.css.bjdt_core.validation.service.ReportExplainValidationService;
import com.css.bjdt_core.validation.service.ValidationInfoService;

/**
 * 报表数据校核
 */
public class ReportDataValidator {
	private static ReportDataValidator instance;

	private CellDataService cellDataSvc;

	private ReportTemplateValidationService validationSvc;

	private ValidationInfoService validationInfoSvc;

	private ReportCache cache = ReportCache.getInstance();

	private RptDealWithService rptDealWithServiceSvc;

	private ReportExplainValidationService reportExplainValidationServiceSvc;

	private ReportTemplateExplainService reportTemplateExplainServiceSvc;

	private static final Log logger = LogFactory.getLog(ReportDataValidator.class);

	private ReportDataValidator() {
	};

	public static synchronized ReportDataValidator getInstance() {
		if (instance == null) {
			instance = new ReportDataValidator();
		}
		return instance;
	}

	public void validate(List<RptTemplateUnitRela> templateUnitRelas, boolean isBatch) {
		if (templateUnitRelas != null && templateUnitRelas.size() > 0) {
			ExecutorService pool = ExecutorPool.getInstance().getPool();
			List<ValidationInfo> validations = new ArrayList<ValidationInfo>();
			final String annualId = templateUnitRelas.get(0).getAnnualId();
			final String dzzId = templateUnitRelas.get(0).getReportUnitId();
			final String batchId = templateUnitRelas.get(0).getBatchId();
			List<ReportDataValidateTread> tasks = new ArrayList<ReportDataValidateTread>();

			if (isBatch) {
				validationInfoSvc.deleteValidationInfoByDzzId(annualId, dzzId);
				List<CellData> cellDatas = cellDataSvc.getCellData(annualId, dzzId, null);
				Map<String, CellData> cellIdMap = new HashMap<>();
				Map<String, List<CellData>> templateMap = new HashMap<>();
				for (CellData data : cellDatas) {
					cellIdMap.put(data.getCellId(), data);
					if (!templateMap.containsKey(data.getTemplateId())) {
						templateMap.put(data.getTemplateId(), new ArrayList<CellData>());
					}
					List datas = templateMap.get(data.getTemplateId());
					datas.add(data);
				}

				List<RptBatchTemplateRel> rptBatchTemplateRelList = rptDealWithServiceSvc
						.queryBatchTemplateRel(batchId);
				Map<String, String> validationStates = new HashMap<>();
				if (rptBatchTemplateRelList != null) {
					for (RptBatchTemplateRel rel : rptBatchTemplateRelList) {
						validationStates.put(rel.getTemplateId(), rel.getValidationState());
					}
				}
				for (RptTemplateUnitRela templateUnitRela : templateUnitRelas) {
					//按报表分线程，先判定当前这个报表属于什么状态的，是未校核、校核出错，还是只通过表内校核
					String validationState = validationStates.get(templateUnitRela.getTemplateId());
					if (validationState != null && validationState.equals("3")) {
						templateUnitRela.setValidationReportType("1");
					} else {
						templateUnitRela.setValidationReportType("2");
					}
					ReportDataValidateTread reportDataValidateTread = new ReportDataValidateTread(templateUnitRela,
							annualId, dzzId, cellIdMap, templateMap);
					tasks.add(reportDataValidateTread);
				}
				try {
					List<Future<List<ValidationInfo>>> resList = pool.invokeAll(tasks);
					for (Future<List<ValidationInfo>> fu : resList) {
						if (fu.get() != null && fu.get().size() != 0) {
							validations.addAll(fu.get());
						}
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				for (RptTemplateUnitRela templateUnitRela : templateUnitRelas) {
					//按报表分线程，先判定当前这个报表属于什么状态的，是未校核、校核出错，还是只通过表内校核
					RptBatchTemplateRel rptBatchTemplateRel = validationSvc
							.getARptBatchTemplateRel(templateUnitRela.getBatchId(), templateUnitRela.getTemplateId());
					if (rptBatchTemplateRel.getValidationState().equals("3")) {
						templateUnitRela.setValidationReportType("1");
					} else {
						templateUnitRela.setValidationReportType("2");
					}
					validationInfoSvc.deleteValidationInfo(annualId, dzzId, templateUnitRela.getTemplateId());
					/*validateByTemplate(templateUnitRela);*/
					List<ValidationInfo> validationInfoList = validateByTemplate(templateUnitRela, null, null);
					validations.addAll(validationInfoList);
				}

			}

			validationInfoSvc.batchInsertValidationInfo(annualId, validations);
		}
	}

	/**
	 *
	 * @param exps 校核集
	 * @param cellDatas 数据集
	 * @param templateUnitRela 年度、单位、报表关系
	 */
	//TODO IMP refactor
	private List<ValidationInfo> validateInnerCell(List<Exp> exps, List<CellData> cellDatas,
			RptTemplateUnitRela templateUnitRela) {
		List<ValidationInfo> validations = new ArrayList<ValidationInfo>();
		//用于取
		List<Cell> cells = (List<Cell>) cache.get(ReportCache.VALIDATE_CELL_CACHE, templateUnitRela.getTemplateId());
		Map<String, CellData> cellDataMap = new HashMap<String, CellData>();
		if (cellDatas != null && cellDatas.size() > 0) {
			for (CellData cellData : cellDatas) {
				cellDataMap.put(cellData.getCellId(), cellData);
			}
		}
		for (Exp exp : exps) {
			//获取校核表达式
			String actualExp = exp.getCellExp();
			String actualExpValidation = exp.getCellExp();
			//按关系运算符(><=)拆分
			String[] array = actualExp.split(ReportConstant.OPERATOR_RELA);
			//生成参加校核公式的指标id
			String[] cellIds = (String[]) ArrayUtils.addAll(array[0].split(ReportConstant.OPERATOR_ARITH),
					array[1].split(ReportConstant.OPERATOR_ARITH));
			String validationState = "2";//未通过
			//加个变量，判断当前的校核公式中的单元格是否有为空的单元，即type为7的，如果有，就不算校核了，也不入库了
			if (cellIds != null && cellIds.length > 0) {
				List<String> cids = new ArrayList<String>();
				for (String cellId : cellIds) {
					CellData cellData = cellDataMap.get(cellId);
					if (null == cellData) {
						cellData = new CellData();
						cellData.setCellId(cellId);
						cellData.setCellValue("0");
						cellData.setCellType("100");
					}
					String key = cellData.getCellId();
					//如果指标值不为null并且校核表达式包含key,那么将其替换
					if (cellData.getCellValue() != null && actualExp.contains(key)) {
						try {
							actualExp = actualExp.replace(key, CellType.cellNull.getKey().equals(cellData.getCellType())
									? "0" : cellData.getCellValue());
							actualExpValidation = actualExpValidation.replace(key,
									CellType.cellNull.getKey().equals(cellData.getCellType()) ? "0"
											: strMultiply100(cellData.getCellValue()));
						} catch (Exception e) {
							e.printStackTrace();
							System.out.println("有问题的格：" + cellData.getCellId());
							continue;
						}
					}
				}
				try {
					if (!actualExpValidation.contains("--")) {
						validationState = "true".equals(MVEL.eval(actualExpValidation).toString()) ? "1" : "2";
					} else {
						validationState = "1000";
					}
				} catch (Exception e) {
					logger.debug("error exp is " + actualExp, e);
				}

			}
			ValidationInfo validationInfo = new ValidationInfo();
			validationInfo.setBatchId(templateUnitRela.getId());
			if (StringUtils.isNotBlank(exp.getFormulaComment())) {
				validationInfo.setSourceExp(exp.getFormulaComment());
			} else {
				validationInfo.setSourceExp(exp.getFormulaShow());
			}
			validationInfo.setCellExp(exp.getCellExp());
			validationInfo.setActualExp(actualExp);
			validationInfo.setValidationType(exp.getFormulaType());
			validationInfo.setValidationState(validationState);
			validationInfo.setCellId(null);
			validationInfo.setDzzId(templateUnitRela.getReportUnitId());
			validationInfo.setTemplateId(templateUnitRela.getTemplateId());
			//判断当前校核公式是否错误，如果错误看看他有没有关联的报表说明，如果没有就完了，如果有就看一下报表说明有没有数
			if (exp.getIsRelationExplain() != "" && exp.getIsRelationExplain() != null
					&& exp.getIsRelationExplain().equals("1") && validationState.equals("2")) {
				RptTemplateExplain rptTemplateExplain = reportTemplateExplainServiceSvc
						.getARptTemplateExplain(exp.getRelationExplainId());
				//如果校核公式错误了，如果有报表说明，那么就认为该校核公式通过校核了，如果没有报表说明，就需要告诉前台填写报表说明或者改数
				String aExplainCount = "";
				if (rptTemplateExplain.getExplainType().equals("1")) {
					aExplainCount = reportExplainValidationServiceSvc.getTiaoMuCountAExplain(
							templateUnitRela.getAnnualId(), templateUnitRela.getSesssionDzzId(),
							exp.getRelationExplainId());
				} else {
					aExplainCount = reportExplainValidationServiceSvc.getShuziWenbenAExplain(
							templateUnitRela.getAnnualId(), templateUnitRela.getSesssionDzzId(),
							exp.getRelationExplainId());
				}
				if (!aExplainCount.equals("0")) {
					validationInfo.setValidationState("1");
				} else {

					String actualExpText = "当前" + exp.getFormulaComment() + "校核不通过,请修改报表数值或修改该报表说明("
							+ rptTemplateExplain.getTemplateExplainName() + ")";
					validationInfo.setActualExp(actualExpText);
				}
			}
			if (!validationState.equals("1000")) {
				validations.add(validationInfo);
			}
		}
		return validations;
	}

	/**
	 * 
	 * @param exps 校核集
	 * @param cellDatas 数据集
	 * @param templateUnitRela 年度、单位、报表关系
	 * @param cellId 指标id
	 */
	private List<ValidationInfo> validateInnerRow(List<Exp> exps, List<CellData> cellDatas,
			RptTemplateUnitRela templateUnitRela, Map<String, Cell> cellColRowMap, Map<String, Cell> cellRowColMap) {
		List<ValidationInfo> validations = new ArrayList<ValidationInfo>();
		//用于取
		List<Cell> cells = (List<Cell>) cache.get(ReportCache.VALIDATE_CELL_CACHE, templateUnitRela.getTemplateId());
		//将缓存中的cells转换成map形式；这种形式[列_行]，还有一种就是[行_列]

		Map<String, CellData> cellDataMap = new HashMap<String, CellData>();
		if (cellDatas != null && cellDatas.size() > 0) {
			for (CellData cellData : cellDatas) {
				cellDataMap.put(cellData.getCellId(), cellData);
			}
		}
		for (Exp exp : exps) {
			//获取校核表达式
			String actualExp = exp.getCellExp();
			String actualExpValidation = exp.getCellExp();
			String validationState = "2";//未通过
			//按关系运算符(><=)拆分
			String[] array = actualExp.split(ReportConstant.OPERATOR_RELA);
			//生成参加校核公式的指标id
			String[] cellIds = (String[]) ArrayUtils.addAll(array[0].split(ReportConstant.OPERATOR_ARITH),
					array[1].split(ReportConstant.OPERATOR_ARITH));

			//加个变量，判断当前的校核公式中的单元格是否有为空的单元，即type为7的，如果有，就不算校核了，也不入库了
			if (cellIds != null && cellIds.length > 0) {
				List<String> cids = new ArrayList<String>();
				for (String cellId : cellIds) {
					Cell cellQuery = new Cell();
					cellQuery = cellRowColMap.get(cellId);
					if (null == cellQuery) {
						cellQuery = new Cell();
					}
					CellData cellData = cellDataMap.get(cellQuery.getCellId());
					if (null == cellData) {
						cellData = new CellData();
						cellData.setCellId(cellId);
						cellData.setCellValue("0");
						cellData.setCellType("100");
					}
					String key = cellId;
					//String key = cellData.getCellId();
					//如果指标值不为null并且校核表达式包含key,那么将其替换
					if (cellData.getCellValue() != null && actualExp.contains(key)) {
						try {
							actualExp = actualExp.replace(key, CellType.cellNull.getKey().equals(cellData.getCellType())
									? "0" : cellData.getCellValue());
							actualExpValidation = actualExpValidation.replace(key,
									CellType.cellNull.getKey().equals(cellData.getCellType()) ? "0"
											: strMultiply100(cellData.getCellValue()));
						} catch (Exception e) {
							e.printStackTrace();
							System.out.println(
									"有问题的格：" + cellData.getAbsColNum() + "*************" + cellData.getAbsRowNum());
							continue;
						}
					}
					try {
						if (!actualExpValidation.contains("--")) {
							validationState = "true".equals(MVEL.eval(actualExpValidation).toString()) ? "1" : "2";
						} else {
							validationState = "1000";
						}
					} catch (Exception e) {
						logger.debug("error exp is " + actualExp, e);
					}
				}
			}

			ValidationInfo validationInfo = new ValidationInfo();
			validationInfo.setBatchId(templateUnitRela.getId());
			if (StringUtils.isNotBlank(exp.getFormulaComment())) {
				validationInfo.setSourceExp(exp.getFormulaComment());
			} else {
				validationInfo.setSourceExp(exp.getFormulaShow());
			}
			validationInfo.setCellExp(exp.getCellExp());
			validationInfo.setActualExp(actualExp);
			validationInfo.setValidationType(exp.getFormulaType());
			validationInfo.setValidationState(validationState);
			validationInfo.setCellId(null);
			validationInfo.setDzzId(templateUnitRela.getReportUnitId());
			validationInfo.setTemplateId(templateUnitRela.getTemplateId());
			if (!validationState.equals("1000")) {
				validations.add(validationInfo);
			}
		}
		return validations;
	}

	/**
	 * 按报表校核指标数据
	 * @param templateUnitRela
	 */
	private List<ValidationInfo> validateByTemplate(RptTemplateUnitRela templateUnitRela,
			Map<String, CellData> cellIdMap, Map<String, List<CellData>> templateMap) {
		List<ValidationInfo> validations = new ArrayList<ValidationInfo>();
		List<CellData> cellDatas = null;
		if (templateMap != null) {
			cellDatas = templateMap.get(templateUnitRela.getTemplateId());
		} else {
			cellDatas = cellDataSvc.getCellData(templateUnitRela.getAnnualId(), templateUnitRela.getReportUnitId(),
					templateUnitRela.getTemplateId());
		}
		//if (cellDatas != null && cellDatas.size() > 0) {
		if (templateUnitRela.getValidationReportType().equals("1")) {
			validations.addAll(validateTableInnerRowExpAndColExp(cellDatas, templateUnitRela));
			validations.addAll(validateTableInnerCellExp(cellDatas, templateUnitRela));
			//validations.addAll(validateTableOuterCellExp(templateUnitRela));
		} else if (templateUnitRela.getValidationReportType().equals("2")) {
			validations.addAll(validateTableInnerRowExpAndColExp(cellDatas, templateUnitRela));
			validations.addAll(validateTableInnerCellExp(cellDatas, templateUnitRela));
			validations.addAll(validateTableOuterCellExp(templateUnitRela, cellIdMap));
		} else {
		}
		//}
		return validations;
	}

	/**
	 * 表内行列校核
	 * @param cellDatas
	 * @param templateUnitRela
	 */
	private List<ValidationInfo> validateTableInnerRowExpAndColExp(List<CellData> cellDatas,
			RptTemplateUnitRela templateUnitRela) {
		List<ValidationInfo> validations = new ArrayList<ValidationInfo>();
		List<Cell> cells = (List<Cell>) cache.get(ReportCache.VALIDATE_CACHE, templateUnitRela.getTemplateId());
		List<Cell> cellsGy = (List<Cell>) cache.get(ReportCache.VALIDATE_CELL_CACHE, templateUnitRela.getTemplateId());
		Map<String, Cell> cellColRowMap = new HashMap<String, Cell>();
		Map<String, Cell> cellRowColMap = new HashMap<String, Cell>();
		for (Cell cell : cellsGy) {
			//行列的
			String cellRowCol = "[" + cell.getRelRowNum() + "_" + cell.getRelColNum() + "]";
			cellRowColMap.put(cellRowCol, cell);
			//列行的
			String cellColRow = "[" + cell.getRelColNum() + "_" + cell.getRelRowNum() + "]";
			cellColRowMap.put(cellColRow, cell);
		}
		for (Cell cell : cells) {
			validations.addAll(validateInnerRow(cell.getValidationExps(), cellDatas, templateUnitRela, cellColRowMap,
					cellRowColMap));
		}
		return validations;
	}

	/**
	 * 表内单元格校核
	 * @param cellDatas
	 * @param templateUnitRela
	 */
	private List<ValidationInfo> validateTableInnerCellExp(List<CellData> cellDatas,
			RptTemplateUnitRela templateUnitRela) {
		List<Exp> validationExps = validationSvc.getValidationExpByType(templateUnitRela.getTemplateId(),
				ValidationType.inner_cell);
		return validateInnerCell(validationExps, cellDatas, templateUnitRela);
	}

	/**
	 * 表间单元格校核
	 * @param templateUnitRela
	 */
	private List<ValidationInfo> validateTableOuterCellExp(RptTemplateUnitRela templateUnitRela,
			Map<String, CellData> cellIdMap) {
		List<Exp> validationExps = validationSvc.getValidationExpByType(templateUnitRela.getTemplateId(),
				ValidationType.outer_cell);
		return validateOuterExp(validationExps, templateUnitRela, cellIdMap);
	}

	public void setCellDataSvc(CellDataService cellDataSvc) {
		this.cellDataSvc = cellDataSvc;
	}

	public void setValidationInfoSvc(ValidationInfoService validationInfoSvc) {
		this.validationInfoSvc = validationInfoSvc;
	}

	public void setRptDealWithServiceSvc(RptDealWithService rptDealWithServiceSvc) {
		this.rptDealWithServiceSvc = rptDealWithServiceSvc;
	}

	public void setValidationSvc(ReportTemplateValidationService validationSvc) {
		this.validationSvc = validationSvc;
	}

	public void setReportExplainValidationServiceSvc(ReportExplainValidationService reportExplainValidationServiceSvc) {
		this.reportExplainValidationServiceSvc = reportExplainValidationServiceSvc;
	}

	public void setReportTemplateExplainServiceSvc(ReportTemplateExplainService reportTemplateExplainServiceSvc) {
		this.reportTemplateExplainServiceSvc = reportTemplateExplainServiceSvc;
	}

	public String strMultiply100(String cellValue) {
		BigDecimal cellValueNum = new BigDecimal(cellValue);
		BigDecimal cellValueEnd = cellValueNum.multiply(new BigDecimal(100));
		return cellValueEnd.toString();
	}

	/**
	 * 表间校核使用
	 * @param exps 校核集
	 * @param templateUnitRela 年度、单位、报表关系
	 */
	//TODO IMP refactor
	private List<ValidationInfo> validateOuterExp(List<Exp> exps, RptTemplateUnitRela templateUnitRela,
			Map<String, CellData> cellIdMap) {
		List<ValidationInfo> validations = new ArrayList<ValidationInfo>();
		//用于取
		for (Exp exp : exps) {
			//获取校核表达式
			String actualExp = exp.getCellExp();
			String actualExpValidation = exp.getCellExp();
			//按关系运算符(><=)拆分
			String[] array = actualExp.split(ReportConstant.OPERATOR_RELA);
			//生成参加校核公式的指标id
			String[] cellIds = (String[]) ArrayUtils.addAll(array[0].split(ReportConstant.OPERATOR_ARITH),
					array[1].split(ReportConstant.OPERATOR_ARITH));
			//获取相关指标数据
			List<CellData> cellDatas = null;
			if (cellIdMap != null) {
				cellDatas = new ArrayList<>();
				for (String cellId : cellIds) {
					if (!cellIdMap.containsKey(cellId)) {
						CellData cellData = new CellData();
						cellData.setCellId(cellId);
						cellData.setCellValue("0");
						cellData.setCellType("100");
						cellDatas.add(cellData);
					} else {
						cellDatas.add(cellIdMap.get(cellId));
					}
				}
			} else {
				cellDatas = cellDataSvc.getAllCellDataByCellIds(templateUnitRela.getAnnualId(),
						templateUnitRela.getReportUnitId(), cellIds);
			}
			String validationState = "2";//未通过
			//加个变量，判断当前的校核公式中的单元格是否有为空的单元，即type为7的，如果有，就不算校核了，也不入库了
			if (cellDatas != null && cellDatas.size() > 0) {
				for (CellData cellData : cellDatas) {
					String key = cellData.getCellId();
					//如果指标值不为null并且校核表达式包含key,那么将其替换
					if (cellData.getCellValue() != null && actualExp.contains(key)) {
						try {
							actualExp = actualExp.replace(key, CellType.cellNull.getKey().equals(cellData.getCellType())
									? "0" : cellData.getCellValue());
							actualExpValidation = actualExpValidation.replace(key,
									CellType.cellNull.getKey().equals(cellData.getCellType()) ? "0"
											: strMultiply100(cellData.getCellValue()));
						} catch (Exception e) {
							e.printStackTrace();
							System.out.println("有问题的格：" + cellData.getCellId());
							continue;
						}
					}
				}
				try {
					if (!actualExpValidation.contains("--")) {
						validationState = "true".equals(MVEL.eval(actualExpValidation).toString()) ? "1" : "2";
					} else {
						validationState = "1000";
					}
				} catch (Exception e) {
					logger.debug("error exp is " + actualExp, e);
				}
			}
			ValidationInfo validationInfo = new ValidationInfo();
			validationInfo.setBatchId(templateUnitRela.getId());
			if (StringUtils.isNotBlank(exp.getFormulaComment())) {
				validationInfo.setSourceExp(exp.getFormulaComment());
			} else {
				validationInfo.setSourceExp(exp.getFormulaShow());
			}
			validationInfo.setCellExp(exp.getCellExp());
			validationInfo.setActualExp(actualExp);
			validationInfo.setValidationType(exp.getFormulaType());
			validationInfo.setValidationState(validationState);
			validationInfo.setCellId(null);
			validationInfo.setDzzId(templateUnitRela.getReportUnitId());
			validationInfo.setTemplateId(templateUnitRela.getTemplateId());
			//判断当前校核公式是否错误，如果错误看看他有没有关联的报表说明，如果没有就完了，如果有就看一下报表说明有没有数
			if (exp.getIsRelationExplain() != "" && exp.getIsRelationExplain() != null
					&& exp.getIsRelationExplain().equals("1") && validationState.equals("2")) {
				RptTemplateExplain rptTemplateExplain = reportTemplateExplainServiceSvc
						.getARptTemplateExplain(exp.getRelationExplainId());
				//如果校核公式错误了，如果有报表说明，那么就认为该校核公式通过校核了，如果没有报表说明，就需要告诉前台填写报表说明或者改数
				String aExplainCount = "";
				if (rptTemplateExplain.getExplainType().equals("1")) {
					aExplainCount = reportExplainValidationServiceSvc.getTiaoMuCountAExplain(
							templateUnitRela.getAnnualId(), templateUnitRela.getSesssionDzzId(),
							exp.getRelationExplainId());
				} else {
					aExplainCount = reportExplainValidationServiceSvc.getShuziWenbenAExplain(
							templateUnitRela.getAnnualId(), templateUnitRela.getSesssionDzzId(),
							exp.getRelationExplainId());
				}
				if (!aExplainCount.equals("0")) {
					validationInfo.setValidationState("1");
				} else {

					String actualExpText = "当前" + exp.getFormulaComment() + "校核不通过,请修改报表数值或修改该报表说明("
							+ rptTemplateExplain.getTemplateExplainName() + ")";
					validationInfo.setActualExp(actualExpText);
				}
			}
			if (!validationState.equals("1000")) {
				validations.add(validationInfo);
			}
		}
		return validations;
	}

	/*public void destory() {
		if (validations != null) {
			validations.clear();
			validations = null;
		}
	}*/

	class ReportDataValidateTread implements Callable<List<ValidationInfo>> {

		private RptTemplateUnitRela templateUnitRela;
		private String annualId;
		private String dzzId;
		private Map<String, CellData> cellIdMap;
		private Map<String, List<CellData>> templateMap;

		public ReportDataValidateTread(RptTemplateUnitRela templateUnitRela, String annualId, String dzzId,
				Map<String, CellData> cellIdMap, Map<String, List<CellData>> templateMap) {
			this.templateUnitRela = templateUnitRela;
			this.annualId = annualId;
			this.dzzId = dzzId;
			this.cellIdMap = cellIdMap;
			this.templateMap = templateMap;
		}

		@Override
		public List<ValidationInfo> call() {
			// TODO Auto-generated method stub
			List<ValidationInfo> validations = validateByTemplate(templateUnitRela, cellIdMap, templateMap);
			logger.debug("------------------------------------->ReportDataValidator 校核的一个线程结束了");
			return validations;
		}
	}

}
